<?php

namespace app\common\model;

class UserScoreRecord extends Base
{
    /**
     * 字典
     */
    const DICT = [
        'source' => [
            '1' => '注册账号',
            '2' => '登录账号',
            '3' => '绑定微信',
            '4' => '绑定邮箱',
            '5' => '分享好友',
            '6' => '普通签到',
            '7' => '连续签到',
            '8' => '激励视频',
            '9' => '桌面访问',
            '10' => '收藏访问',
            '11' => '绑公众号',
            '12' => '绑定手机',
            '13' => '扫码加群',
            '14' => '公众号签到',
        ]
    ];

    /**
     * add
     * @param $uid
     * @param $type
     * @param $score
     * @param $source
     * @param $remark
     * @return integer|string
     */
    public function add($uid, $type, $score, $source = 0, $remark = '')
    {
        $data = [
            'uid' => $uid,
            'type' => $type,
            'score' => $score,
            'source' => $source,
            'remark' => $remark,
            'add_time' => time(),
        ];
        return $this->insert($data);
    }

    /**
     * 查询用户当天类型积分
     * @param int $uid
     * @param int $type
     * @return float
     */
    public function getTodayCountByUid(int $uid, int $type): float
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $uid],
            ['type', '=', $type],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        return $this->where($where)->sum('score');
    }

    /**
     * 查询信息
     * @param int $uid
     * @return array
     * @throws
     */
    public function getListByUid(int $uid): array
    {
        //查询数据
        $where = [['uid', '=', $uid]];
        $list = $this->where($where)->limit(3)->order('id', 'desc')->select();

        //处理数据
        $dict = self::DICT;
        foreach ($list as $key => $val) {
            if ($val['type'] == 2) {
                $list[$key]['score'] = "-{$val['score']}";
                $list[$key]['source_name'] = "题库消费";
            } else {
                $list[$key]['score'] = "+{$val['score']}";
                $list[$key]['source_name'] = $dict['source'][$val['source']] ?? '-';
            }
            $list[$key]['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
        }

        return $list ? $list->toArray() : [];
    }

    /**
     * 检查当天签到
     * @param $userId
     * @return bool
     */
    public function checkTodaySign($userId): bool
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $userId],
            ['source', 'in', [6, 7]],
            ['type', '=', 1],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        return (bool)$this->where($where)->count();
    }

    /**
     * 检查当天是否看视频
     * @param $userId
     * @return bool
     */
    public function checkTodayReward($userId): bool
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $userScoreRecord = new UserScoreRecord();
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 8],
            ['type', '=', 1],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        $count = $userScoreRecord->where($where)->count();
        if ($count >= 10000) {
            return false;
        }
        return true;
    }

    /**
     * 检查是否已经绑定公众号
     * @param $userId
     * @return bool
     */
    public function checkMp($userId): bool
    {
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 11],
            ['type', '=', 1],
        ];
        return (bool)$this->where($where)->count();
    }

    /**
     * 检查是否已经绑定桌面
     * @param $userId
     * @return bool
     */
    public function checkDesktop($userId): bool
    {
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 9],
            ['type', '=', 1],
        ];
        return (bool)$this->where($where)->count();
    }

    /**
     * checkCount
     * @param $userId
     * @param $source
     * @return int
     * @throws
     */
    public function checkCount($userId, $source)
    {
        $where = [
            ['uid', '=', $userId],
            ['source', '=', $source],
            ['type', '=', 1],
        ];
        return $this->where($where)->count();
    }

    /**
     * checkSource
     * @param $userId
     * @param $source
     * @return bool
     */
    public function checkSource($userId, $source): bool
    {
        $where = [
            ['uid', '=', $userId],
            ['source', '=', $source],
            ['type', '=', 1],
        ];
        return (bool)$this->where($where)->count();
    }


    /**
     * 检查昨天签到
     * @param $userId
     * @return bool
     */
    public function checkYesTodaySign($userId): bool
    {
        $y_s_time = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $y_e_time = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 6],
            ['type', '=', 1],
            ['add_time', 'between', [$y_s_time, $y_e_time]],
        ];
        return (bool)$this->where($where)->count();
    }

    /**
     * 获取当天记录数量
     * @param $userId
     * @return bool
     */
    public function getTodayCount($userId, $source): bool
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $userId],
            ['source', 'in', $source],
            ['type', '=', 1],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        return $this->where($where)->count();
    }
}